// Decompiled with JetBrains decompiler
// Type: FibMatrix.Config.ConfigFormatUtil
// Assembly: FMCommon, Version=1.0.8663.21262, Culture=neutral, PublicKeyToken=null
// MVID: 52773074-FB09-4BA0-8FA9-4782D11D6C17
// Assembly location: /Users/mac/work/RiverGame/com.rivergame.fmframework/Runtime/Plugins/FMCommon.dll

using System;
using System.IO;
using System.Runtime.CompilerServices;

namespace FibMatrix.Config
{
  public class ConfigFormatUtil
  {
    private static char[] _separator = new char[1]{ '|' };
    public const int LITTLE_INT_MAX_VALUE = 536870911;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static int ReadInt32(byte[] buffers, int pos)
    {
      return (int) buffers[pos] | (int) buffers[pos + 1] << 8 | (int) buffers[pos + 2] << 16 | (int) buffers[pos + 3] << 24;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static int ReadInt32(Stream buffers, int pos)
    {
      buffers.Seek((long) pos, SeekOrigin.Begin);
      int num1 = buffers.ReadByte();
      int num2 = buffers.ReadByte();
      int num3 = buffers.ReadByte();
      int num4 = buffers.ReadByte();
      int num5 = num2 << 8;
      return num1 | num5 | num3 << 16 | num4 << 24;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static int ReadInt32(byte[] buffers, ref int pos)
    {
      int index = pos;
      pos += 4;
      return (int) buffers[index] | (int) buffers[index + 1] << 8 | (int) buffers[index + 2] << 16 | (int) buffers[index + 3] << 24;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static unsafe int ReadInt32(byte* buffers, ref int pos)
    {
      buffers += pos;
      pos += 4;
      return (int) *buffers | (int) buffers[1] << 8 | (int) buffers[2] << 16 | (int) buffers[3] << 24;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static ushort ReadUInt16(byte[] buffers, ref int pos)
    {
      int index = pos;
      pos += 2;
      return (ushort) ((uint) buffers[index] | (uint) buffers[index + 1] << 8);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static unsafe ushort ReadUInt16(byte* buffers, ref int pos)
    {
      buffers += pos;
      pos += 2;
      return (ushort) ((uint) *buffers | (uint) buffers[1] << 8);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte ReadByte(byte[] buffers, ref int pos)
    {
      int index = pos;
      ++pos;
      return buffers[index];
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static unsafe byte ReadByte(byte* buffers, ref int pos)
    {
      int index = pos;
      ++pos;
      return buffers[index];
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static unsafe int ReadInt32(byte* buffers, ref int pos, byte byteNum)
    {
      buffers += pos;
      pos += (int) byteNum;
      switch (byteNum)
      {
        case 1:
          return (int) *buffers;
        case 2:
          return (int) *buffers | (int) buffers[1] << 8;
        case 3:
          return (int) *buffers | (int) buffers[1] << 8 | (int) buffers[2] << 16;
        case 4:
          return (int) *buffers | (int) buffers[1] << 8 | (int) buffers[2] << 16 | (int) buffers[3] << 24;
        default:
          return 0;
      }
    }

    public static int Read7BitEncodedInt(byte[] buffers, ref int pos)
    {
      int num1 = 0;
      int num2 = 0;
      while (num2 != 35)
      {
        byte num3 = ConfigFormatUtil.ReadByte(buffers, ref pos);
        num1 |= ((int) num3 & (int) sbyte.MaxValue) << num2;
        num2 += 7;
        if (((int) num3 & 128) == 0)
          return num1;
      }
      throw new FormatException("Format_Bad7BitInt32");
    }

    public static unsafe int Read7BitEncodedInt(byte* buffers, ref int pos)
    {
      int num1 = 0;
      int num2 = 0;
      while (num2 != 35)
      {
        byte num3 = ConfigFormatUtil.ReadByte(buffers, ref pos);
        num1 |= ((int) num3 & (int) sbyte.MaxValue) << num2;
        num2 += 7;
        if (((int) num3 & 128) == 0)
          return num1;
      }
      throw new FormatException("Format_Bad7BitInt32");
    }

    public static void Write7BitEncodedInt(BinaryWriter writer, int value)
    {
      uint num;
      for (num = (uint) value; num >= 128U; num >>= 7)
        writer.Write((byte) (num | 128U));
      writer.Write((byte) num);
    }

    public static void Write7BitEncodedInt(MemoryStream ms, int value)
    {
      uint num;
      for (num = (uint) value; num >= 128U; num >>= 7)
        ms.WriteByte((byte) (num | 128U));
      ms.WriteByte((byte) num);
    }

    public static int Read7BitEncodedInt(BinaryReader reader)
    {
      int num1 = 0;
      int num2 = 0;
      while (num2 != 35)
      {
        byte num3 = reader.ReadByte();
        num1 |= ((int) num3 & (int) sbyte.MaxValue) << num2;
        num2 += 7;
        if (((int) num3 & 128) == 0)
          return num1;
      }
      throw new FormatException("Format_Bad7BitInt32");
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void Write(byte[] buffer, int pos, int ivalue)
    {
      uint num = (uint) ivalue;
      buffer[pos] = (byte) num;
      buffer[pos + 1] = (byte) (num >> 8);
      buffer[pos + 2] = (byte) (num >> 16);
      buffer[pos + 3] = (byte) (num >> 24);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void Write(MemoryStream ms, int ivalue)
    {
      uint num = (uint) ivalue;
      ms.WriteByte((byte) num);
      ms.WriteByte((byte) (num >> 8));
      ms.WriteByte((byte) (num >> 16));
      ms.WriteByte((byte) (num >> 24));
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void Write(byte[] buffer, int pos, ushort value)
    {
      buffer[pos] = (byte) value;
      buffer[pos + 1] = (byte) ((uint) value >> 8);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void Write(MemoryStream ms, ushort value)
    {
      ms.WriteByte((byte) value);
      ms.WriteByte((byte) ((uint) value >> 8));
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void Write(MemoryStream ms, int ivalue, byte byteNum)
    {
      uint num1 = (uint) ivalue;
      if (byteNum <= (byte) 0)
        return;
      int num2 = (int) byteNum;
      uint num3 = (uint) (1UL << (int) byteNum * 8) - 1U;
      uint num4 = num1 & num3;
      do
      {
        ms.WriteByte((byte) num4);
        num4 >>= 8;
        --num2;
      }
      while (num2 > 0);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void Write(byte[] buffer, int pos, int ivalue, byte byteNum)
    {
      uint num1 = (uint) ivalue;
      if (byteNum <= (byte) 0)
        return;
      int num2 = (int) byteNum;
      uint num3 = (uint) ((ulong) (1L << (int) byteNum * 8) - 1UL);
      uint num4 = num1 & num3;
      do
      {
        buffer[pos] = (byte) num4;
        ++pos;
        num4 >>= 8;
        --num2;
      }
      while (num2 > 0);
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte[] ConvertToBytes(byte[] buffer, int value, byte byteNum)
    {
      ConfigFormatUtil.Write(buffer, 0, value, byteNum);
      return buffer;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte[] ConvertToBytes(byte[] buffer, int value)
    {
      ConfigFormatUtil.Write(buffer, 0, value);
      return buffer;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte[] ConvertToBytes(byte[] buffer, int value, int offset)
    {
      ConfigFormatUtil.Write(buffer, offset, value);
      return buffer;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static byte[] ConvertToBytes(byte[] buffer, ushort value)
    {
      ConfigFormatUtil.Write(buffer, 0, value);
      return buffer;
    }

    public static int[] SplitStringToIntArray(string rawString, char seperator)
    {
      if (string.IsNullOrEmpty(rawString))
        return (int[]) null;
      ConfigFormatUtil._separator[0] = seperator;
      string[] strArray = rawString.Split(ConfigFormatUtil._separator);
      int length = strArray.Length;
      if (length <= 0)
        return (int[]) null;
      int[] numArray = new int[strArray.Length];
      for (int index = 0; index < length; ++index)
        numArray[index] = int.Parse(strArray[index]);
      return numArray;
    }

    public static float[] SplitStringToFloatArray(string rawString, char seperator)
    {
      if (string.IsNullOrEmpty(rawString))
        return (float[]) null;
      ConfigFormatUtil._separator[0] = seperator;
      string[] strArray = rawString.Split(ConfigFormatUtil._separator);
      int length = strArray.Length;
      if (length <= 0)
        return (float[]) null;
      float[] numArray = new float[strArray.Length];
      for (int index = 0; index < length; ++index)
        numArray[index] = float.Parse(strArray[index]);
      return numArray;
    }

    public static string[] SplitString(string rawString, char seperator)
    {
      if (string.IsNullOrEmpty(rawString))
        return (string[]) null;
      ConfigFormatUtil._separator[0] = seperator;
      return rawString.Split(ConfigFormatUtil._separator);
    }

    public static string[] ConvertIntArrayToStringArray(int[] intArray)
    {
      int length = intArray.Length;
      string[] strArray = new string[length];
      for (int index = 0; index < length; ++index)
        strArray[index] = intArray[index].ToString();
      return strArray;
    }

    public static int[] ConvertStringArrayToIntArray(string[] stringArray)
    {
      int length = stringArray.Length;
      int[] numArray = new int[length];
      for (int index = 0; index < length; ++index)
        numArray[index] = int.Parse(stringArray[index]);
      return numArray;
    }

    public static string[] ConvertFloatArrayToStringArray(float[] floatArray)
    {
      int length = floatArray.Length;
      string[] strArray = new string[length];
      for (int index = 0; index < length; ++index)
        strArray[index] = floatArray[index].ToString();
      return strArray;
    }

    public static float[] ConvertStringArrayToFloatArray(string[] stringArray)
    {
      int length = stringArray.Length;
      float[] numArray = new float[length];
      for (int index = 0; index < length; ++index)
        numArray[index] = float.Parse(stringArray[index]);
      return numArray;
    }

    public static byte EncodeIntByteLen(byte byteLen)
    {
      switch (byteLen)
      {
        case 0:
          return 0;
        case 1:
          return 1;
        case 2:
          return 2;
        default:
          return 3;
      }
    }

    public static byte DecodeIntByteLen(byte encodeVal)
    {
      switch (encodeVal)
      {
        case 0:
          return 0;
        case 1:
          return 1;
        case 2:
          return 2;
        default:
          return 4;
      }
    }

    public static byte EncodeOtherByteLen(byte byteLen)
    {
      return byteLen;
    }

    public static byte DecodeOtherByteLen(byte encodeVal)
    {
      return encodeVal;
    }
  }
}
